//
//  HDPayoutHeaderView.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/4/12.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}

protocol HDheaderViewDelegate:class {
   func  headerViewBtnClick()
    func noticeImageClick()
}

class HDPayoutHeaderView: UIView {
    weak var delegate:HDheaderViewDelegate?
    var showBtn:UIButton = UIButton()
    var payoutStatus:String!
    var widthofLabel_X:CGFloat = 0
    var casefailModelArray = [HDPayoutCasefailModel]() // 问题列表
    var heightOfLabel_Height:CGFloat = 0

    var heightOfHeaderView_Y:CGFloat = 20
    var localIssueNumber:String = "" // 本地问题列表
    
    var is_dishonorStr:String = ""
    var dishonor_introduceStr:String = ""
    var showMoney:NSNumber = 0
    var moneyView = HDHopeModeyView()
    
    func setupHeaderView(){
        widthofLabel_X = 0
        if payoutStatus != "-1" && payoutStatus != "0" {
            widthofLabel_X += 10.0
        }
        
        setupMessage()
        if self.casefailModelArray.count>1 {
            for index in 0..<self.casefailModelArray.count - 1 {
                let model = self.casefailModelArray[index]
                
                
                
               let carseLabel = createNameLabel(heightOfHeaderView_Y ,nameLable_X:33 , textStr: model.introduce, font: 13)
                carseLabel.textColor = UIColor(hex:0x666666)
                let temp = carseLabel.text!.getTextRectSize(model.introduce as NSString, font: UIFont.systemFont(ofSize: 13), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
                
                heightOfHeaderView_Y += temp.size.height + 2
            }
            
        }
        heightOfHeaderView_Y += 18
        if payoutStatus == "3" {
            if self.is_dishonorStr != "0" {
                
                createLabelWithIconImageView("赔案通知书", nameLable_Y: heightOfHeaderView_Y, imagename: "icon赔案通知书")
                heightOfHeaderView_Y += 25
                
                let noticeImageView = UIImageView()
                addSubview(noticeImageView)
                noticeImageView.snp.makeConstraints { (make) in
                    make.top.equalTo(heightOfHeaderView_Y)
                    make.left.equalTo(55)
                    make.width.equalTo(62)
                    make.height.equalTo(73)
                }
                noticeImageView.image = UIImage(named: "缩略图")
                noticeImageView.isUserInteractionEnabled = true
                heightOfHeaderView_Y += 18 + 73
                
                let tapGesture = UITapGestureRecognizer(target: self,action: #selector(HDPayoutHeaderView.noticeViewTap(_:)))
                noticeImageView.addGestureRecognizer(tapGesture)
            }else{
                createLabelWithIconImageView("撤单原因", nameLable_Y: heightOfHeaderView_Y, imagename: "撤单")
                heightOfHeaderView_Y += 30
                
                let dishonorLabel = UILabel()
                self.addSubview(dishonorLabel)
                dishonorLabel.text = self.dishonor_introduceStr
                let temp = dishonorLabel.text?.getTextRectSize(dishonorLabel.text! as NSString, font: UIFont.systemFont(ofSize: 13), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
                dishonorLabel.snp.makeConstraints { (make) in
                    make.top.equalTo(heightOfHeaderView_Y + 1)
                    make.left.equalTo(32)
                    make.width.equalTo(AppConstant.screenWidth - 60)
                    make.height.equalTo((temp?.height)! + 5)
                }
                dishonorLabel.font = UIFont.systemFont(ofSize: 13)
                dishonorLabel.numberOfLines = 0
                dishonorLabel.textColor = UIColor(hex:0x666666)
                dishonorLabel.textAlignment = .left
                heightOfHeaderView_Y += (temp?.height)! + 18
            }
            
        }
    

        let classifyImageView = UIImageView()
        self.addSubview(classifyImageView)
        classifyImageView.snp.makeConstraints { (make) in
            make.top.equalTo(heightOfHeaderView_Y)
            make.left.equalTo(widthofLabel_X + 15)
            make.height.equalTo(19)
            make.width.equalTo(19)
        }
        classifyImageView.image = UIImage(named: "资料分类_icon")
        classifyImageView.isUserInteractionEnabled = true
        
        let classifyLabel = UILabel()
        self.addSubview(classifyLabel)
        classifyLabel.snp.makeConstraints { (make) in
            make.top.equalTo(heightOfHeaderView_Y - 10)
            make.left.equalTo(widthofLabel_X + 39)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(40)
        }
        classifyLabel.text = "资料分类"
        classifyLabel.textAlignment = .left
        classifyLabel.font = UIFont.systemFont(ofSize: 16)
        classifyLabel.textColor = UIColor(hex:0x333333)
        classifyLabel.isUserInteractionEnabled = true
        
        
        
        self.addSubview(showBtn)
        
        showBtn.snp.makeConstraints { (make) in
            make.top.equalTo(heightOfHeaderView_Y)
            make.left.equalTo(AppConstant.screenWidth - 45)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
        showBtn.backgroundColor = UIColor.white
        
        if payoutStatus != "-1" && payoutStatus != "0"{
            showBtn.addTarget(self, action:#selector(HDPayoutHeaderView.btnClick), for: .touchUpInside)
            
            showBtn.setImage(UIImage(named: "下拉展开_icon"), for: UIControlState())
            
            let labelTapGesture = UITapGestureRecognizer(target: self,action: #selector(HDPayoutHeaderView.btnClick))
            classifyLabel.addGestureRecognizer(labelTapGesture)
        }
        
        
        heightOfHeaderView_Y += 30
        
        //
         if Int(self.localIssueNumber)>0 {
            let iconFuzzy = UIImageView()
            self.addSubview(iconFuzzy)
            iconFuzzy.snp.makeConstraints({ (make) in
                make.top.equalTo(heightOfHeaderView_Y + 2)
                make.left.equalTo(34)
                make.width.equalTo(13)
                make.height.equalTo(13)
            })
            
            iconFuzzy.image = UIImage(named: "模糊提示")
            let nameLabel = UILabel()
            nameLabel.text = "有\(localIssueNumber)张图片模糊，可能会影响赔付结果，建议重新拍照上传"
            let temp = nameLabel.text?.getTextRectSize(nameLabel.text! as NSString, font: UIFont.systemFont(ofSize: 13), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
            self.addSubview(nameLabel)
            nameLabel.snp.makeConstraints { (make) in
                make.top.equalTo(heightOfHeaderView_Y)
                make.left.equalTo(50)
                make.width.equalTo(AppConstant.screenWidth - 60)
                make.height.equalTo((temp?.height)! + 2)
            }
            nameLabel.textAlignment = .left
            nameLabel.numberOfLines = 0
            nameLabel.font = UIFont.systemFont(ofSize: 13)
            nameLabel.textColor = UIColor(hex:0x666666)
            
            
            heightOfHeaderView_Y += (temp?.height)! + 15
        }
        
        
       self.frame = CGRect(x: 0, y: 0, width: AppConstant.screenWidth, height: heightOfHeaderView_Y)
        
        if (payoutStatus != "-1"&&payoutStatus != "0") {
            createLine()
        }
        
        
    }
    
    func setupMessage(){
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(heightOfHeaderView_Y)
            make.left.equalTo(15 - 2)
            make.height.equalTo(11)
            make.width.equalTo(11)
        }
        payIconImageView.image = UIImage(named: "赔付申请")
        
        heightOfLabel_Height = 0
        var statusStr = "申请赔付"
        var promptStr = "点击每类资料下方“＋”，添加赔付资料并申请提交"
        if (payoutStatus == "0"){
            statusStr = "申请赔付"
            
        }else if (payoutStatus == "1"||payoutStatus == "6"){
            statusStr = "审核中"
            promptStr = "已经开始审核，请耐心等待"
        }else if payoutStatus == "2"||payoutStatus == "5"{
            statusStr = "有问题"
            
            let model = self.casefailModelArray.last
            promptStr = model!.introduce
            payIconImageView.image = UIImage(named: "有问题标识")
            
        }else if payoutStatus == "3"{
            statusStr = "预结案"
            if self.is_dishonorStr == "0" {
                promptStr = "已撤单，详情查看撤单原因"
            }else if self.is_dishonorStr == "2"{
                promptStr = "已拒付，详情见赔案通知书"
            }else{
                promptStr = "已预结案，详情查看赔案通知书"
            }
            
        }else if payoutStatus == "4"{
            statusStr = "已结案"
            promptStr = "已给付，详情见赔案通知书"
            
            
        }else if payoutStatus == "7"{
            statusStr = "退回"
            let model = self.casefailModelArray.first
            promptStr = "退回说明：" + (model?.introduce)!
            
            
        }
        
        let statusLabel = createNameLabel(heightOfHeaderView_Y - 5,nameLable_X:33 , textStr: statusStr, font: 18)
        heightOfHeaderView_Y += 20
        
        if payoutStatus == "0" || payoutStatus == "-1" {
            addSubview(moneyView)
            moneyView.snp.makeConstraints({ (make) in
                make.top.equalTo(statusLabel.snp.bottom).offset(21)
                make.left.equalTo(statusLabel.snp.left)
                make.right.equalTo(-31)
                make.height.equalTo(31)
                
            })
            moneyView.moneyNumber = showMoney.stringValue
            moneyView.setupTextFieldView()
            heightOfHeaderView_Y += 31 + 21 + 5
        }
        if payoutStatus == "2" {
            statusLabel.textColor = UIColor(hex:0xfd3f3f)
            
        }else{
            statusLabel.textColor = ConstantColor.appThemeColor
            
        }
        if (payoutStatus != "0" && payoutStatus != "-1") {
            //
            let temp = promptStr.getTextRectSize(promptStr as NSString, font: UIFont.systemFont(ofSize: 13), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
            
            let promptLabel = createNameLabel(heightOfHeaderView_Y ,nameLable_X:33 , textStr: promptStr, font: 13)
            promptLabel.textColor = UIColor(hex:0x666666)
            addSubview(moneyView)
            moneyView.snp.makeConstraints({ (make) in
                make.top.equalTo(promptLabel.snp.bottom).offset(8)
                make.left.equalTo(promptLabel.snp.left)
                make.right.equalTo(-31)
                make.height.equalTo(13)
                
            })
            moneyView.moneyNumber = showMoney.stringValue
            moneyView.setupMessageView()
            heightOfHeaderView_Y += 13 + 20
            heightOfHeaderView_Y += temp.height + 15
        }




    }
    
    func createNameLabel(_ nameLable_Y:CGFloat,nameLable_X:CGFloat,textStr:String,font:CGFloat)->UILabel{
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        
        nameLabel.text = textStr
        nameLabel.numberOfLines = 0
        let temp = nameLabel.text!.getTextRectSize(textStr as NSString, font: UIFont.systemFont(ofSize: font), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y)
            make.left.equalTo(nameLable_X)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(temp.height + 2)
        }
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: font)
        
        return nameLabel
    }
    
    func btnClick(){
        delegate?.headerViewBtnClick()
    }
    
    func createLabelWithIconImage(_ text:String,nameLable_Y:CGFloat,imageName:String){
        
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y + heightOfLabel_Height)
            make.left.equalTo(widthofLabel_X + 22)
            make.height.equalTo(13)
            make.width.equalTo(13)
        }
        payIconImageView.image = UIImage(named: imageName) //
        
        let promptLabel = createNameLabel(nameLable_Y + heightOfLabel_Height,nameLable_X:widthofLabel_X + 40, textStr: text, font: 13)
        promptLabel.textColor = UIColor(hex:0x666666)
    }
    
    func createLabelWithIconImageView(_ text:String,nameLable_Y:CGFloat,imagename:String){
        
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y)
            make.left.equalTo(25)
            make.height.equalTo(19)
            make.width.equalTo(19)
        }
        payIconImageView.image = UIImage(named: imagename) //
        
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y + 1)
            make.left.equalTo(payIconImageView.snp.right).offset(5)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(16)
        }
        nameLabel.text = text
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: 16)
        
        nameLabel.textColor = UIColor(hex:0x333333)
    }
    
    func createLine(){
        let lineLabel = UILabel()
        self.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.left.equalTo(18)
            make.width.equalTo(1)
            make.bottom.equalTo(0)
        }
        lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
    }

    func backViewHeight()->CGFloat{
        return self.frame.size.height
    }
    
    func noticeViewTap(_ gesture:UITapGestureRecognizer){
        delegate?.noticeImageClick()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        moneyView.moneyTextField.resignFirstResponder()
    }

}
